import { request } from '@/service/request';

/** ETL任务DTO */
export interface EtlTaskDTO {
  id?: number;
  name: string;
  taskType: string;
  sourceId: number;
  targetId: number;
  sourceConfig?: string;
  targetConfig?: string;
  transformConfig?: string;
  scheduleConfig?: string;
  status?: number;
  projectId?: number;
  remark?: string;
}

/** ETL任务VO */
export interface EtlTaskVO {
  id: number;
  name: string;
  taskType: string;
  sourceId: number;
  sourceName?: string;
  targetId: number;
  targetName?: string;
  sourceConfig?: string;
  targetConfig?: string;
  transformConfig?: string;
  scheduleConfig?: string;
  dsWorkflowId?: number;
  status: number;
  projectId?: number;
  createTime: string;
  updateTime: string;
  remark?: string;
}

/** ETL实例VO */
export interface EtlInstanceVO {
  id: number;
  taskId: number;
  taskName?: string;
  dsInstanceId?: number;
  status: string;
  startTime: string;
  endTime?: string;
  duration?: number;
  sourceRows?: number;
  targetRows?: number;
  successRows?: number;
  failedRows?: number;
  errorMessage?: string;
  logs?: string;
  createTime: string;
}

/** ETL执行进度 */
export interface EtlProgress {
  instanceId: number;
  status: string;
  percentage: number;
  currentStep: string;
  processedRows?: number;
  totalRows?: number;
  errorMessage?: string;
}

/**
 * 创建ETL任务
 */
export function createEtlTask(data: EtlTaskDTO) {
  return request<number>({
    url: '/dataplatform/etl/tasks',
    method: 'post',
    data
  });
}

/**
 * 更新ETL任务
 */
export function updateEtlTask(id: number, data: EtlTaskDTO) {
  return request<void>({
    url: `/dataplatform/etl/tasks/${id}`,
    method: 'put',
    data
  });
}

/**
 * 删除ETL任务
 */
export function deleteEtlTask(id: number) {
  return request<void>({
    url: `/dataplatform/etl/tasks/${id}`,
    method: 'delete'
  });
}

/**
 * 获取ETL任务详情
 */
export function getEtlTask(id: number) {
  return request<EtlTaskVO>({
    url: `/dataplatform/etl/tasks/${id}`,
    method: 'get'
  });
}

/**
 * 分页查询ETL任务列表
 */
export function getEtlTaskList(params: Api.Common.CommonSearchParams) {
  return request<Api.Common.CommonListResponse<EtlTaskVO>>({
    url: '/dataplatform/etl/tasks',
    method: 'get',
    params
  });
}

/**
 * 执行ETL任务
 */
export function executeEtlTask(id: number) {
  return request<number>({
    url: `/dataplatform/etl/tasks/${id}/execute`,
    method: 'post'
  });
}

/**
 * 停止ETL任务
 */
export function stopEtlTask(instanceId: number) {
  return request<void>({
    url: `/dataplatform/etl/tasks/instances/${instanceId}/stop`,
    method: 'post'
  });
}

/**
 * 获取执行进度
 */
export function getEtlProgress(instanceId: number) {
  return request<EtlProgress>({
    url: `/dataplatform/etl/tasks/instances/${instanceId}/progress`,
    method: 'get'
  });
}

/**
 * 分页查询任务实例列表
 */
export function getEtlInstanceList(params: Api.Common.CommonSearchParams & { taskId?: number }) {
  return request<Api.Common.CommonListResponse<EtlInstanceVO>>({
    url: '/dataplatform/etl/tasks/instances',
    method: 'get',
    params
  });
}
